package study.saas.order.service;

import org.apache.poi.hssf.record.PageBreakRecord;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service;
import study.saas.common.util.CopyClassUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.order.entity.Commodity;
import study.saas.order.entity.Orders;
import study.saas.order.repo.CommodityRepo;
import study.saas.order.repo.OrdersRepo;
import study.saas.order.service.model.OrdersMakeDTO;
import study.saas.order.service.model.OrdersSearchDTO;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author zxx
 */
@Service
public class OrdersService {
    @Resource
    private OrdersRepo ordersRepo;

    @Resource
    private CommodityRepo commodityRepo;

    public ApiResultV2 make(OrdersMakeDTO make) {
        Commodity commodity = commodityRepo.findFirstBycommodityName(make.getCommodityName());
        if (commodity==null) {
            return ApiResultV2.error("该商品不存在");
        }
        Orders orders = ordersRepo.findFirstByCommodityName(make.getCommodityName());
        if (make.getAmount()>commodity.getAmount()) {
            return ApiResultV2.error("库存不足");
        }
        BigDecimal p = commodity.getPrice();
        BigDecimal t=BigDecimal.valueOf(make.getAmount()).multiply(p);
        Integer newAmount = commodity.getAmount()-make.getAmount();
        commodity.setAmount(newAmount);
        commodityRepo.save(commodity);
        Orders newOrders=new Orders();
        newOrders.setPrice(p);
        newOrders.setTotalPrice(t);
        CopyClassUtil.copyClass(make,newOrders);
        ordersRepo.save(newOrders);
        return ApiResultV2.ok();
    }

    public ApiResultV2 getByUserName(String userName) {
        List<Orders> ordersList = ordersRepo.findAllByUserName(userName);
        Orders orders = ordersRepo.findFirstByUserName(userName);
        if(orders==null) {
            return ApiResultV2.error("还没有订单记录哦");
        }
        for (Orders orders1: ordersList) {
            if (orders1 == null) {
                return ApiResultV2.error("无相关订单");
            }
        }
        return ApiResultV2.ok(ordersList);
    }

    public ApiResultV2 getByCommodityName(String commodityName) {
        List<Orders> ordersList = ordersRepo.findAllByCommodityName(commodityName);
        Orders orders = ordersRepo.findFirstByCommodityName(commodityName);
        if(orders==null) {
            return ApiResultV2.error("还没有订单记录哦");
        }
        for (Orders orders1: ordersList) {
            if (orders1 == null) {
               return ApiResultV2.error("无相关订单");
            }
        }
        return ApiResultV2.ok(ordersList);

    }

    public ApiResultV2 search(OrdersSearchDTO search) {
        List<Orders> ordersList1 = ordersRepo.findAllByUserName(search.getUserName());
        Orders orders = ordersRepo.findFirstByUserName(search.getUserName());
        List<Orders> ordersList2 = ordersRepo.findAllByCommodityName(search.getCommodityName());
        if (orders == null) {
            return ApiResultV2.error("还没有订单记录哦");
        }
        for (Orders orders1:ordersList1){
            for (Orders orders2:ordersList2){
                if (orders1.getUserName().equals(orders2.getUserName())&&orders1.getCommodityName().equals(orders2.getCommodityName())){
                    return ApiResultV2.ok(orders1);
                }
            }
        }
        return ApiResultV2.error("无相关订单");
    }

}
